package com.tanhua.app.controller;


import com.aliyuncs.exceptions.ClientException;
import com.tanhua.app.service.QuanZiMqService;
import com.tanhua.app.service.QuanziService;
import com.tanhua.domain.mogo.Publish;
import com.tanhua.domain.vo.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@RestController //@ResponseBody ＋ @Controller只返回json数据 不返回jsp页面
public class QuanZiController {

    @Autowired
    private QuanziService quanziService;

    @Autowired
    private QuanZiMqService quanZiMqService;

    @PostMapping(value = "/movements", name = "发布动态")

    //定义发布动态方法 接收参数publish 图片(数组) 对应quanzi_publish(动态发布)表
    public ResponseEntity movements(Publish publish, MultipartFile[] imageContent) throws IOException {
        //返回参数:发布内容,图片地址(数组)
        return quanziService.movements(publish, imageContent);
    }

    @GetMapping(value = "/movements/all", name = "查看我的动态")
    public ResponseEntity findAlbumByPage(Integer page, Integer pagesize, Long userId) throws IllegalAccessException, InvocationTargetException {
        return quanziService.findAlbumByPage(page, pagesize, userId);
    }


    @GetMapping(value = "/movements", name = "查看好友动态")
    public ResponseEntity findTimeLineByPage(Integer page, Integer pagesize) throws IllegalAccessException, InvocationTargetException {
        //返回分页数据和用户id
        return quanziService.findTimeLineByPage(page, pagesize, UserHolder.getUser().getId());
    }

    @GetMapping(value = "/movements/recommend", name = "查看我的推荐动态")
    public ResponseEntity findRecommendByPage(Integer page, Integer pagesize) throws ClientException, IOException {
        return quanziService.findRecommendByPage(page, pagesize, UserHolder.getUser().getId());
    }

    //用户行为数据  将用户行为数据记录到消息中间件
    /*- 操作1： 浏览 +1
      - 操作2： 点赞 +5
      - 操作3： 喜欢 +8
      - 操作4： 评论 + 10
      - 操作5：取消点赞 -5
      - 操作6：取消喜欢 -8*/

    @GetMapping(value = "/movements/{id}/like", name = "点赞")
    public ResponseEntity commentLike(@PathVariable("id") String publishId) throws ClientException, IOException {
        //存入 点赞消息数据
        quanZiMqService.SetMsg(publishId,2);
        return quanziService.commentLike(publishId);
    }

    @GetMapping(value = "/movements/{id}/dislike", name = "取消点赞")
    public ResponseEntity dislikeLike(@PathVariable("id") String publishId) throws ClientException, IOException {

        quanZiMqService.SetMsg(publishId,5);
        return quanziService.dislike(publishId);
    }

    @GetMapping(value = "/movements/{id}/love", name = "喜欢")
    public ResponseEntity commentLove(@PathVariable("id") String publishId) throws ClientException, IOException {
        quanZiMqService.SetMsg(publishId,3);
        return quanziService.commentLove(publishId);
    }

    @GetMapping(value = "/movements/{id}/unlove", name = "取消喜欢")
    public ResponseEntity dislikeLove(@PathVariable("id") String publishId) throws ClientException, IOException {
        quanZiMqService.SetMsg(publishId,6);
        return quanziService.disLove(publishId);
    }

    @GetMapping(value = "/movements/{id}", name = "查看评论--动态回显")
    public ResponseEntity findByPublishId(@PathVariable("id") String publishId) throws ClientException, IOException {
        quanZiMqService.SetMsg(publishId,1);
        return quanziService.findByPublishId(publishId);
    }

    @GetMapping(value = "/comments", name = "查看评论--评论回显")
    public ResponseEntity findComments(String movementId, Integer page, Integer pagesize) throws ClientException, IOException {

        return quanziService.findComments(movementId, page, pagesize);
    }

    @PostMapping(value = "/comments", name = "发表评论")
    public ResponseEntity addComments(@RequestBody Map<String, String> map) throws ClientException, IOException {
        String publishId = map.get("movementId");
        String content = map.get("comment");
        quanZiMqService.SetMsg(publishId,4);
        return quanziService.addComments(publishId, content);
    }

}
